<?php
/**
 * Created by PhpStorm.
 * User: jiangxiaobo
 * Date: 2019/11/11 0011
 * Time: 21:18
 */
namespace bootstrap\lib;

use bootstrap\lib\traits\Singleton;

class Request
{
    use Singleton;

    protected $envConfig = null;

    public function globals()
    {
        $numargs = func_num_args();
        //$args = func_get_args();
        switch($numargs)
        {
            case 0:
                return $GLOBALS;
                break;
            case 1:
                return $GLOBALS[func_get_arg(0)];
                break;
            case 2:
            default:
                return $GLOBALS[func_get_arg(0)] = $GLOBALS[func_get_arg(1)];
        }
    }

    /**
     * $_SERVER 不可写入
     * @return mixed
     */
    public function server()
    {
        switch(func_num_args())
        {
            case 0:
                return $_SERVER;
                break;
            case 1:
                $_1 = func_get_arg(0);
                return isset($_SERVER[$_1])?$_SERVER[$_1]:null;
                break;
            case 2:
            default:
                $_1 = func_get_arg(0);
                $_2 = func_get_arg(1);
                return isset($_SERVER[$_1])?$_SERVER[$_1]:$_SERVER[$_2];
        }
    }

    public function get()
    {
        switch(func_num_args())
        {
            case 0:
                return $_GET;
                break;
            case 1:
                $_1 = func_get_arg(0);
                return isset($_GET[$_1])?$_GET[$_1]:null;
                break;
            case 2:
            default:
                $_1 = func_get_arg(0);
                $_2 = func_get_arg(1);
                return isset($_GET[$_1])?$_GET[$_1]:$_GET[$_2];
        }
    }

    public function post()
    {
        switch(func_num_args())
        {
            case 0:
                return $_POST;
                break;
            case 1:
                $_1 = func_get_arg(0);
                return isset($_POST[$_1])?$_POST[$_1]:null;
                break;
            case 2:
            default:
                $_1 = func_get_arg(0);
                $_2 = func_get_arg(1);
                return isset($_POST[$_1])?$_POST[$_1]:$_POST[$_2];
        }
    }

    public function files()
    {
        switch(func_num_args())
        {
            case 0:
                return $_FILES;
                break;
            case 1:
            default:
                $_1 = func_get_arg(0);
                return isset($_FILES[$_1])?$_FILES[$_1]:null;
        }
    }

    public function cookie()
    {

    }

    public function session()
    {

    }

    public function request()
    {
        switch(func_num_args())
        {
            case 0:
                return $_REQUEST;
                break;
            case 1:
                $_1 = func_get_arg(0);
                return isset($_REQUEST[$_1])?$_REQUEST[$_1]:null;
                break;
            case 2:
            default:
                $_1 = func_get_arg(0);
                $_2 = func_get_arg(1);
                return isset($_REQUEST[$_1])?$_REQUEST[$_1]:$_REQUEST[$_2];
        }
    }

    public function env()
    {
        if(is_file('../.env'))
        {
            $env = parse_ini_file(ROOT_PATH . '.env', true);
            foreach ($env as $key => $val) {
                $name = ENV_PREFIX . strtoupper($key);

                if (is_array($val)) {
                    foreach ($val as $k => $v) {
                        $item = $name . '_' . strtoupper($k);
                        putenv("$item=$v");
                    }
                } else {
                    putenv("$name=$val");
                }
            }
        }
        switch(func_num_args())
        {
            case 0:
                return $_ENV;
                break;
            case 1:
                return $_ENV[func_get_arg(0)];
                break;
            case 2:
            default:
                return $_ENV[func_get_arg(0)] = $_ENV[func_get_arg(1)];
        }
    }

    public function isAjax()
    {
        return false;
    }
}
